
#include "archivingFiles.h"

#include "config.h"
#include "readWriteFile.h"

#include <zip.h>
#include <filesystem>


const_int archivingFile(const_ch *nameFile, const_ch *archiveName) {
    zip_t *zip = zip_open(archiveName, ZIP_CREATE | ZIP_TRUNCATE, NULL);

    zip_source_t *source = zip_source_file(zip, nameFile, 0, -1);
    zip_file_add(zip, nameFile, source, ZIP_FL_ENC_UTF_8);

    zip_close(zip);

    return 0;
}

const_int archivingFile(vector<str> nameFiles, const_ch* archiveName) {
    zip_t *zip = zip_open(archiveName, ZIP_CREATE | ZIP_TRUNCATE, NULL);

    FOR_SIZE_T_ADDREVIATED(i, nameFiles.size()) {
        zip_source_t *source = zip_source_file(zip, nameFiles[i].c_str(), 0, -1);
        zip_file_add(zip, nameFiles[i].c_str(), source, ZIP_FL_ENC_UTF_8);
    }
    
    zip_close(zip);
    
    return 0;
}

const_int archivingFolder(const_ch* nameFolder, const_ch* archiveName) {
    vector<str> nameFiles;
    filesystem::path folderPath(nameFolder);

    if (filesystem::exists(folderPath) && filesystem::is_directory(folderPath)) {
        for (const auto& entry : filesystem::directory_iterator(folderPath)) {
            if (entry.is_regular_file()) {
                nameFiles.push_back(entry.path());
            }
        }
    } else {
        cout << ERROR_MSG << endl;
        return 1;
    }

    if (archivingFile(nameFiles, archiveName)) {
        cout << ERROR_MSG << endl;
        return 1;
    }
    
    return 0;
}


const_int deleteArchive(const_ch* archiveName) {
    if (!remove(archiveName)) return 1;

    return 0;
}


